﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class OptionPanel : BasePanel
{
    private CanvasGroup canvasGroup;
    private AudioSource music;
    public Slider slider1;
    public Slider slider2;
    public Button effectsOnButton;
    public Button effectsOffButton;
    public Button musicOnButton;
    public Button musicOffButton;
    private float tempEffectsVolume;
    private float tempMusicVolume;
    private bool _opening;
    private bool _closing;

    private void Awake()
    {
        if (canvasGroup == null)
            canvasGroup = this.GetComponent<CanvasGroup>();

        music = Camera.main.GetComponent<AudioSource>();
        slider1.value = PlayerPrefs.GetFloat("EffectsVolume"); //音效音量本地读取
        slider2.value = PlayerPrefs.GetFloat("MusicVolume"); //音效音量本地读取
        EffectButtonChoose();
        MusicButtonChoose();
        tempEffectsVolume = 0.5f;
        tempMusicVolume = 0.5f;
        _opening = false;
        _closing = false;
    }
    private void Update()
    {
        if(_opening)
        {
            canvasGroup.alpha += Time.unscaledDeltaTime / 0.3f;
            if (canvasGroup.alpha >= 1)
                _opening = false;
        }
        if(_closing)
        {
            canvasGroup.alpha -= Time.unscaledDeltaTime / 0.3f;
            if (canvasGroup.alpha <= 0)
                _closing = false;
        }
    }
    public override void OnEnter()
    {
        if (canvasGroup == null)
            canvasGroup = this.GetComponent<CanvasGroup>();
        canvasGroup.blocksRaycasts = true;
        canvasGroup.interactable = true;

        if(music == null)
            music = Camera.main.GetComponent<AudioSource>();

        canvasGroup.alpha = 0f;
        _opening = true;

    }

    public override void OnExit()
    {
        if (canvasGroup == null)
            canvasGroup = this.GetComponent<CanvasGroup>();
        canvasGroup.blocksRaycasts = false;
        canvasGroup.interactable = false;
        _closing = true;
    }

    public override void OnPause()
    {
        if (canvasGroup == null)
            canvasGroup = this.GetComponent<CanvasGroup>();
        canvasGroup.blocksRaycasts = false;
        canvasGroup.interactable = false;
    }

    public override void OnResume()
    {
        if (canvasGroup == null)
            canvasGroup = this.GetComponent<CanvasGroup>();
        canvasGroup.blocksRaycasts = true;
        canvasGroup.interactable = true;
    }

    public void CloseButton() //关闭按钮
    {
        EffectObjectManager.Instance.ButtonEffects();
        UIManager.Instance.PopPanel();
    }

    public void Slider1() //音效音量
    {
        PlayerPrefs.SetFloat("EffectsVolume", slider1.value); //音效音量本地存储
        PlayerPrefs.Save();
        EffectButtonChoose();
    }

    public void Slider2() //音乐音量
    {
        music.volume = slider2.value;
        PlayerPrefs.SetFloat("MusicVolume", slider2.value); //音效音量本地存储
        PlayerPrefs.Save();
        MusicButtonChoose();
    }
    public void EffectsOnButton()
    {
        tempEffectsVolume = PlayerPrefs.GetFloat("EffectsVolume");
        PlayerPrefs.SetFloat("EffectsVolume", 0f); //音效音量本地存储
        slider1.value = 0f;
        PlayerPrefs.Save();
        effectsOnButton.gameObject.SetActive(false);
        effectsOffButton.gameObject.SetActive(true);
    }

    public void EffectsOffButton()
    {
        slider1.value = tempEffectsVolume;
        PlayerPrefs.SetFloat("EffectsVolume", tempEffectsVolume); //音效音量本地存储
        PlayerPrefs.Save();
        effectsOnButton.gameObject.SetActive(true);
        effectsOffButton.gameObject.SetActive(false);
    }
    public void MusicOnButton()
    {
        tempMusicVolume = PlayerPrefs.GetFloat("MusicVolume");
        PlayerPrefs.SetFloat("MusicVolume", 0f); //音效音量本地存储
        slider2.value = 0f;
        PlayerPrefs.Save();
        musicOnButton.gameObject.SetActive(false);
        musicOffButton.gameObject.SetActive(true);
    }
    public void MusicOffnButton()
    {
        slider2.value = tempMusicVolume;
        PlayerPrefs.SetFloat("MusicVolume", tempMusicVolume); //音效音量本地存储
        PlayerPrefs.Save();
        musicOnButton.gameObject.SetActive(true);
        musicOffButton.gameObject.SetActive(false);
    }
    private void EffectButtonChoose()
    {
        if (slider1.value == 0f)
        {
            effectsOnButton.gameObject.SetActive(false);
            effectsOffButton.gameObject.SetActive(true);
        }
        if (slider1.value > 0f)
        {
            effectsOnButton.gameObject.SetActive(true);
            effectsOffButton.gameObject.SetActive(false);
        }
    }
    private void MusicButtonChoose()
    {
        if (slider2.value == 0f)
        {
            musicOnButton.gameObject.SetActive(false);
            musicOffButton.gameObject.SetActive(true);
        }
        if (slider2.value > 0f)
        {
            musicOnButton.gameObject.SetActive(true);
            musicOffButton.gameObject.SetActive(false);
        }
    }
}
